package com.example.springboot_demo.controller;

import com.example.springboot_demo.dto.List2Json;
import com.example.springboot_demo.dto.request.ClazzRoomRequest;
import com.example.springboot_demo.dto.request.UserRequest;
import com.example.springboot_demo.utils.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/6/21:8:24
 */

@SuppressWarnings({"unchecked", ""})
@RequestMapping("/form")
@RestController
@Validated
public class FormController {
    private static final Logger log = LoggerFactory.getLogger(FormController.class);


//    StringValue
    @PostMapping("/StringValue")
    public Object stringValue(@RequestParam("name1") String name,String password){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("name",name);
        hashMap.put("password",password);
        return hashMap;
    }

    @PostMapping("/map")
    public Object stringValue(@RequestParam Map map){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",map);
        return hashMap;
    }

    @PostMapping("/user")
    public Object user(UserRequest userRequest){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",userRequest);
        return hashMap;
    }



    /**
     * 传入一个数组，后端自动封装成List集合
     * @param ids
     */
    @PostMapping("/list")
    public Object list(@RequestParam List<Integer> ids){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("code",200);
        return hashMap;
    }



    /**
     * 传入一个json数组，后端自动封装成List集合
     * {
     *     "name":"xm"
     * }
     * @param list2Json
     */
    @PostMapping("/list2Json")
    public Object list2Json(@RequestBody List2Json list2Json){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",list2Json);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     * 使用json 接收 嵌套参数
    */
//    ClazzUserValue
    @PostMapping("/ClazzUserValue")
    public Object clazzUserValue(@RequestBody ClazzRoomRequest clazzRoomRequest){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",clazzRoomRequest);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     * http://localhost:8099/form/findByIdAndName/123/%E5%B0%8F%E6%98%8E
     * http://localhost:8099/form/findByIdAndName/1/xm
     */
    @GetMapping("/findByIdAndName/{id}/{name}")
    public AjaxResult find(@PathVariable("name") String name, @PathVariable("id") Integer id) {
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("id",id);
        hashMap.put("name",name);
        //AjaxResult success = AjaxResult.success(hashMap);
        AjaxResult success = AjaxResult.error("本次操作异常");
        return success;
    }


//    checkParam
    /**
     * 校验对象
    */
    @PostMapping("/checkParam")
    public AjaxResult checkParam(@RequestBody @Validated UserRequest userRequest){

        return AjaxResult.success(userRequest);
    }



    @GetMapping("/findStudentById")
    public AjaxResult findStudentById( @NotBlank(message = "name 不能为空") String name,
                                       Integer id) {

        AjaxResult success = AjaxResult.success();
        return success;
    }

}
